package com.daiqee.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;

/**
 * session 过期自动提示登录超时
 * 
 * <br/>
 * @since JDK 1.8
 * @version  V2.1
 * @author RichardTang 
 * @date: 2018年5月7日 下午5:52:23
 * @package com.daiqee.filter
 * @copyright:Copyright (c) 2018, 1245811923@qq.com All Rights Reserved.
 */
public class SessionFilter implements Filter {
	
	 @Override
	 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException,
	     ServletException {
	
		 HttpServletRequest request = (HttpServletRequest) servletRequest;
		 
		 HttpServletResponse response = (HttpServletResponse) servletResponse;
		 
		 if (!SecurityUtils.getSubject().isAuthenticated()) {
		     //判断session里是否有用户信息,且是否为ajax请求，如果是ajax请求响应头会有，x-requested-with
		     if (request.getHeader("x-requested-with") != null
		             && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {
		         response.setHeader("session-status", "timeout");//在响应头设置session状态
		     }
		 }
		 filterChain.doFilter(request, servletResponse);
	}
	
	@Override
	public void destroy() {
	 // TODO Auto-generated method stub
	}
	
	@Override
	public void init(FilterConfig arg0) throws ServletException {
	 // TODO Auto-generated method stub
	}
}
